import BTComposite from "../../Base/BTComposite";
import { NodeStatues } from "../../Enum";

export default class CompositeSequence extends BTComposite {

    public onstart(): void {
        super.onstart();
        this.index = 0;

    }

    public canExecute(): boolean {
        return this.index < this.children.length && this.statues !== NodeStatues.Failed;
    }

    public onChildExecute(Statue: NodeStatues): void {
        switch (Statue) {
            case NodeStatues.Success:
                this.index++;
                if (this.index >= this.children.length) {
                    this.statues = NodeStatues.Success;
                } else {
                    this.statues = NodeStatues.Running
                }
                break;
            case NodeStatues.Failed:
                this.statues = NodeStatues.Failed;
                break;
            case NodeStatues.Running:
                this.statues = NodeStatues.Running;
                break;
            default:
                break;
        }
    }
    // public onUpdate(): NodeStatues {

    //     if (this.statues == NodeStatues.Failed) {
    //         return NodeStatues.Failed;
    //     }
    //     if (this.index >= this.children.length) {
    //         return NodeStatues.Failed;
    //     }

    //     const child = this.children[this.index];
    //     const res = child.run();
    //     if (res === NodeStatues.Failed) {
    //         this.statues = NodeStatues.Failed;
    //         return NodeStatues.Failed;
    //     }

    //     if (res === NodeStatues.Success) {
    //         this.index++;
    //         return NodeStatues.Running;
    //     }
    //     return NodeStatues.Running;
    // }
}